package com.example.demo.service.impl;

import com.example.demo.entity.Personnelmanagearea;
import com.example.demo.entity.Personnelmanageseekhelp;
import com.example.demo.handle.SeekHelpListParseHandler;
import com.example.demo.mapper.PersonnelmanageseekhelpMapper;
import com.example.demo.service.PersonnelmanageseekhelpService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hys
 * @since 2021-07-26
 */
@Service
public class PersonnelmanageseekhelpServiceImpl extends ServiceImpl<PersonnelmanageseekhelpMapper, Personnelmanageseekhelp> implements PersonnelmanageseekhelpService {

    @Autowired
    private PersonnelmanageseekhelpMapper personnelmanageseekhelpMapper;

    @Override
    public Integer insertSeekHelpData(String seekHelpXml) {
        //1.获取一个SAXParserFactory的实例对象
        SAXParserFactory factory = SAXParserFactory.newInstance();
        //2.通过factory的newSAXParser()方法获取一个SAXParser类的对象。
        try {
            SAXParser parser = null;
            try {
                parser = factory.newSAXParser();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            }
            //创建SAXParserHandler对象
            SeekHelpListParseHandler seekHelpListParseHandler = new SeekHelpListParseHandler();
            try {
                // 读取xml文件
                parser.parse(seekHelpXml, seekHelpListParseHandler);
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("~~~~~共有" + seekHelpListParseHandler.getPersonnelmanageseekhelpArrayList().size() + "个节点被存储");
            for (Personnelmanageseekhelp personnelmanageseekhelp : seekHelpListParseHandler.getPersonnelmanageseekhelpArrayList()) {
                System.out.println(personnelmanageseekhelp.getCsMineCode());
                System.out.println(personnelmanageseekhelp.getCsDataTime());
                System.out.println(personnelmanageseekhelp.getPsPersonCard());
                System.out.println(personnelmanageseekhelp.getPsMineEntertime());
                System.out.println(personnelmanageseekhelp.getPsAreaCode());
                System.out.println(personnelmanageseekhelp.getPsAreaEntertime());
                System.out.println(personnelmanageseekhelp.getPsStationCode());
                System.out.println(personnelmanageseekhelp.getPsStationEntertime());
                System.out.println(personnelmanageseekhelp.getPsAlarmStime());
                System.out.println(personnelmanageseekhelp.getPsAlarmEtime());
                System.out.println("----finish----");
                personnelmanageseekhelpMapper.insertSeekHelpData(personnelmanageseekhelp.getCsMineCode(),personnelmanageseekhelp.getCsDataTime(),personnelmanageseekhelp.getPsPersonCard(),personnelmanageseekhelp.getPsMineEntertime(),personnelmanageseekhelp.getPsAreaCode(),personnelmanageseekhelp.getPsAreaEntertime(),personnelmanageseekhelp.getPsStationCode(),personnelmanageseekhelp.getPsStationEntertime(),personnelmanageseekhelp.getPsAlarmStime(),personnelmanageseekhelp.getPsAlarmEtime());
            }
        } catch (SAXException e) {
            e.printStackTrace();
        }
        return 1;
    }
}
